package com.sgxy.wmservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgxy.wmservice.entity.WmInputOrder;
import com.sgxy.wmservice.entity.WmStoreGoods;
import com.sgxy.wmservice.entity.vo.InputOrderVo;
import com.sgxy.wmservice.mapper.WmInputOrderMapper;
import com.sgxy.wmservice.service.WmInputOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgxy.wmservice.service.WmStoreGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-29
 */
@Service
public class WmInputOrderServiceImpl extends ServiceImpl<WmInputOrderMapper, WmInputOrder> implements WmInputOrderService {

    @Autowired
    private WmStoreGoodsService wmStoreGoodsService;
    @Override
    public IPage<InputOrderVo> selectPageVoList(IPage<InputOrderVo> page, WmInputOrder wmInputOrder) {
        return baseMapper.selectPageVoList(page,wmInputOrder);
    }

    @Override
    public InputOrderVo selectOneById(String id) {
        return baseMapper.selectOneById(id);
    }

    @Override
    @Transactional
    public void addInputOrder(WmInputOrder wmInputOrder) {
        WmStoreGoods storeGoods;
        QueryWrapper<WmStoreGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",wmInputOrder.getStoreId());
        wrapper.eq("goods_id",wmInputOrder.getGoodsId());
        storeGoods = wmStoreGoodsService.getOne(wrapper);
        if(storeGoods == null){
            storeGoods = new WmStoreGoods();
        }
        storeGoods.setStockNum(storeGoods.getStockNum()+wmInputOrder.getInputNum());
        storeGoods.setGoodsId(wmInputOrder.getGoodsId());
        storeGoods.setStoreId(wmInputOrder.getStoreId());
        baseMapper.insert(wmInputOrder);
        wmStoreGoodsService.saveOrUpdate(storeGoods);
    }

    @Override
    public void updateInputOrder(WmInputOrder wmInputOrder) {
        WmStoreGoods storeGoods;
        QueryWrapper<WmStoreGoods> wrapper = new QueryWrapper<>();
        WmInputOrder beforeWmInputOrder = baseMapper.selectById(wmInputOrder.getId());
        wrapper.eq("store_id",wmInputOrder.getStoreId());
        wrapper.eq("goods_id",wmInputOrder.getGoodsId());
        storeGoods = wmStoreGoodsService.getOne(wrapper);
        storeGoods.setStockNum(storeGoods.getStockNum()-beforeWmInputOrder.getInputNum()+wmInputOrder.getInputNum());
        wmStoreGoodsService.saveOrUpdate(storeGoods);
        baseMapper.updateById(wmInputOrder);
    }

    @Override
    public void deleteById(String id) {
        WmStoreGoods storeGoods;
        QueryWrapper<WmStoreGoods> wrapper = new QueryWrapper<>();
        WmInputOrder wmInputOrder = baseMapper.selectById(id);
        wrapper.eq("store_id",wmInputOrder.getStoreId());
        wrapper.eq("goods_id",wmInputOrder.getGoodsId());
        storeGoods = wmStoreGoodsService.getOne(wrapper);
        storeGoods.setStockNum(storeGoods.getStockNum()-wmInputOrder.getInputNum());
        wmStoreGoodsService.saveOrUpdate(storeGoods);
        baseMapper.deleteById(id);

    }
}
